package com.qinggeng.project.system.service.impl;

import java.util.List;

import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.system.domain.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.qinggeng.project.system.mapper.OrdersRecordMapper;
import com.qinggeng.project.system.domain.OrdersRecord;
import com.qinggeng.project.system.service.IOrdersRecordService;

import javax.annotation.Resource;

/**
 * 订单记录表Service业务层处理
 *
 * @author smallfish
 * @date 2020-12-22
 */
@Slf4j
@Service
public class OrdersRecordServiceImpl implements IOrdersRecordService
{
    @Resource
    private OrdersRecordMapper ordersRecordMapper;

    @Override
    public OrdersRecord getOrdersRecordById(Long id)
    {
        return ordersRecordMapper.getOrdersRecordById(id);
    }

    @Override
    public OrdersRecord getOrdersRecordByOrdersNo(String ordersNo) {
        return ordersRecordMapper.getOrdersRecordByOrdersNo(ordersNo);
    }

    @Override
    public List<OrdersRecord> listOrdersRecord(OrdersRecord ordersRecord)
    {
        return ordersRecordMapper.listOrdersRecord(ordersRecord);
    }

    @Override
    public List<OrdersRecord> listOrdersRecordByPointIdAndRecordDate(Long pointId,String recordDate){
        OrdersRecord where=new OrdersRecord();
        where.setPointId(pointId);
        where.setRecordDate(recordDate);
        where.setSend(0);
        return ordersRecordMapper.listOrdersRecord(where);
    }

    @Override
    public int insertOrdersRecord(OrdersRecord ordersRecord)
    {
        ordersRecord.setCreateTime(DateUtils.getNowDate());
        return ordersRecordMapper.insertOrdersRecord(ordersRecord);
    }

    @Override
    public boolean insertOrdersRecord(Orders orders,Long pointId,String recordDate) {
        OrdersRecord ordersRecord=ordersRecordMapper.getOrdersRecordByOrdersNo(orders.getOrdersNo());
        if(ordersRecord!=null){
            return true;
        }
        ordersRecord=new OrdersRecord();
        ordersRecord.setPointId(pointId);
        ordersRecord.setOrdersNo(orders.getOrdersNo());
        ordersRecord.setUserId(orders.getUserId());
        ordersRecord.setRecordDate(recordDate);
        ordersRecord.setSend(0);
        int rows= this.insertOrdersRecord(ordersRecord);
        if(rows<1){
            log.error("新增订单记录失败");
            throw new CustomException("新增订单记录失败");
        }
        return true;
    }


    @Override
    public boolean deleteOrdersRecordByOrdersNo(String ordersNo){
        OrdersRecord ordersRecord=ordersRecordMapper.getOrdersRecordByOrdersNo(ordersNo);
        if(null==ordersRecord){
            return true;
        }
        return ordersRecordMapper.deleteOrdersRecordById(ordersRecord.getId())>0;
    }

    @Override
    public int updateOrdersRecord(OrdersRecord ordersRecord)
    {
        ordersRecord.setUpdateTime(DateUtils.getNowDate());
        return ordersRecordMapper.updateOrdersRecord(ordersRecord);
    }

    @Override
    public int deleteOrdersRecordByIds(Long[] ids)
    {
        return ordersRecordMapper.deleteOrdersRecordByIds(ids);
    }

    /**
     * 删除订单记录表信息
     *
     * @param id 订单记录表ID
     * @return 结果
     */
    @Override
    public int deleteOrdersRecordById(Long id)
    {
        return ordersRecordMapper.deleteOrdersRecordById(id);
    }

    @Override
    public boolean updateOrdersRecordSendStatus(Long id) {
       OrdersRecord ordersRecord=new OrdersRecord();
       ordersRecord.setId(id);
       ordersRecord.setSend(1);
       return this.updateOrdersRecord(ordersRecord)>0;
    }
}
